package cn.jdx.controller;

import cn.jdx.utils.JdxQqMail;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/mail")
public class SendMail {
    //=====================【获取QQ邮箱验证码】========================

    @Resource
    private JavaMailSenderImpl mailSender;

    @Value("${spring.mail.username}")
    private String fromQq;

    @Async
    @GetMapping("/send")
    public Object send(String qq){
        System.out.println("发送方的QQ:"+fromQq);
        try{
            try {
                Thread.sleep(1000); //阻塞一秒钟然后再发邮件
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            JdxQqMail qqMail = new JdxQqMail(mailSender);
            qqMail.send("AIGame验证码",
                    "<h1>验证码:"+"123456"+"</h1>",
                    true,
                    fromQq,
                    qq+"@qq.com");
            System.out.println("已经发送啦");
            return true;
        }catch (Exception e){
            System.out.println("[发送验证码出现异常]");
            e.printStackTrace();
            return false;
        }
    }
}
